package com.javatip.project.detection.detectionTrainMission.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.javatip.common.constant.IsPublic;
import com.javatip.project.Controller.getUserInfo.controller.GetUserRoleController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.javatip.project.detection.detectionTrainMission.domain.DetectionTrainMission;
import com.javatip.project.detection.detectionTrainMission.service.IDetectionTrainMissionService;
import com.javatip.framework.web.controller.BaseController;
import com.javatip.framework.web.page.TableDataInfo;
import com.javatip.framework.web.domain.AjaxResult;

/**
 * 目标检测训练任务信息操作处理
 *
 * @author 袁伟
 * @date 2024-09-27
 */
@Api(tags = "目标检测训练任务信息管理")
@Controller
@RequestMapping("/detection/detectionTrainMission")
public class DetectionTrainMissionController extends BaseController {
    private String prefix = "detection/detectionTrainMission";

    @Autowired
    private IDetectionTrainMissionService detectionTrainMissionService;

    @Autowired
    private GetUserRoleController getUserRoleController;

    @RequiresPermissions("detection:detectionTrainMission:view")
    @GetMapping()
    public String detectionTrainMission() {
        return prefix + "/detectionTrainMission";
    }

    /**
     * 查询目标检测训练任务列表
     */
    @RequiresPermissions("detection:detectionTrainMission:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation(value = "查询目标检测训练任务列表")
    public TableDataInfo list(@RequestParam(value = "status", required = false) String status) {
//        System.out.println("DetectionTrainMissionController.数据库查询结果: " + detectionTrainMission);
        DetectionTrainMission detectionTrainMission = new DetectionTrainMission();
        startPage();
        if (status != null && !status.isEmpty() && status.contains(",")) {
            status = status.split(",")[0];
        }

        System.out.println("DetectionTrainMissionController.list status: " + status);

        // 获取当前用户 ID
        int userId = getUserRoleController.getUserId().getBody();
        System.out.println("当前用户 ID: " + userId);  // 打印查看是否正确获取

        // 判断是否为管理员
        System.out.println("是否为管理员: " + getUserRoleController.isAdmin().getBody());
        if (getUserRoleController.isAdmin().getBody().equals(true)) {
            System.out.println("当前用户为管理员");
        } else {
            System.out.println("当前用户非管理员");
        }

        // 执行查询
        List<DetectionTrainMission> selfList = detectionTrainMissionService.selectSelfDetectionTrainMissionList(userId, detectionTrainMission);
//        System.out.println("数据库查询结果: " + list);

        // 存储查询结果的集合
        List<DetectionTrainMission> newList = new ArrayList<>();
        if (status.equals("self")) {
//            System.out.println("当前用户id: " + userId);
            return getDataTable(selfList);
        } else if (getUserRoleController.isAdmin().getBody().equals(true)) {
            List<DetectionTrainMission> detectionTrainMissions = detectionTrainMissionService.selectDetectionTrainMissionList(detectionTrainMission);
//            System.out.println("管理员数据库查询结果: " + list);
            return getDataTable(detectionTrainMissions);
        } else {
            List<DetectionTrainMission> detectionTrainMissions = detectionTrainMissionService.selectPublicAndSelfDetectionTrainMissionList(userId, detectionTrainMission);
//            System.out.println("非管理员数据库查询结果: " + newList);
            return getDataTable(detectionTrainMissions);
        }
    }


    /**
     * 新增目标检测训练任务
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存目标检测训练任务
     */
    @RequiresPermissions("detection:detectionTrainMission:add")
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "新增目标检测训练任务")
    public AjaxResult addSave(DetectionTrainMission detectionTrainMission) {
        System.out.println("DetectionTrainMissionController.addSave: " + detectionTrainMission);
        return toAjax(detectionTrainMissionService.insertDetectionTrainMission(detectionTrainMission));
    }

    /**
     * 修改目标检测训练任务
     */
    @ApiOperation(value = "修改目标检测训练任务")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        DetectionTrainMission detectionTrainMission = detectionTrainMissionService.selectDetectionTrainMissionById(id);
        mmap.put("detectionTrainMission", detectionTrainMission);
        return prefix + "/edit";
    }

    /**
     * 修改保存目标检测训练任务
     */
    @RequiresPermissions("detection:detectionTrainMission:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DetectionTrainMission detectionTrainMission) {
        return toAjax(detectionTrainMissionService.updateDetectionTrainMission(detectionTrainMission));
    }

    /**
     * 删除目标检测训练任务
     */
    @ApiOperation(value = "删除目标检测训练任务")
    @RequiresPermissions("detection:detectionTrainMission:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        String property = System.getProperty("user.dir");
        System.out.println("property: " + property);
        String destination = property + "/detectionDataset/" + getUserRoleController.getUserId().getBody() + "/"
                + detectionTrainMissionService.selectDetectionTrainMissionById(Integer.valueOf(ids)).getTrainMissionName();
        return toAjax(detectionTrainMissionService.deleteDetectionTrainMissionByIds(ids));
    }

    /**
     * 启动单一目标检测训练任务
     */

    @PostMapping("/startSingleDetectionTrainMission/{id}")
    @ResponseBody
    @ApiOperation(value = "启动单一目标检测训练任务")
    public AjaxResult startSingleDetectionTrainMission(Integer id) {
        return detectionTrainMissionService.startSingleDetectionTrainMission(id);
    }

}
